package com.heu.blood.dict.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;
import com.heu.blood.dict.dao.DictDepartmentDao;
import com.heu.blood.dict.entity.DictDepartmentEntity;
import com.heu.blood.dict.service.DictDepartmentService;
import com.heu.blood.dict.vo.DepartmentDictVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("dictDepartmentService")
public class DictDepartmentServiceImpl extends ServiceImpl<DictDepartmentDao, DictDepartmentEntity> implements DictDepartmentService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DictDepartmentEntity> page = this.page(
                new Query<DictDepartmentEntity>().getPage(params),
                new QueryWrapper<DictDepartmentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<DepartmentDictVo> getTreeRes() {
        List<DictDepartmentEntity> list = this.list();
        List<DepartmentDictVo> res = new ArrayList<>();
        // 最先遍历一遍找到根节点
        for(DictDepartmentEntity entity : list){
            if(entity.getParentCode().equals("0")){
                DepartmentDictVo vo = new DepartmentDictVo();
                vo.setId(entity.getId());
                vo.setCode(entity.getCode());
                vo.setParentCode(entity.getParentCode());
                vo.setLabel(entity.getName());
                vo.setChildren(new ArrayList<>());
                res.add(vo);
            }
        }
        for(DepartmentDictVo curVo : res){
            process(curVo,list);
        }
        return res;
    }

    @Override
    public int saveWithCheck(DictDepartmentEntity dictDepartment) {
        if(dictDepartment.getParentCode().equals(dictDepartment.getCode())){
            return 2;
        }
        LambdaQueryWrapper<DictDepartmentEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictDepartmentEntity::getCode, dictDepartment.getCode());
        List<DictDepartmentEntity> dictDepartmentEntities = this.baseMapper.selectList(wrapper);
        if(dictDepartmentEntities.size() == 0){
            this.save(dictDepartment);
            return 0;
        }else{
            return 1;
        }

    }

    /**
     * 递归查找方法
     * @param curVo
     * @param list
     */
    public void process(DepartmentDictVo curVo,List<DictDepartmentEntity> list){
        for(DictDepartmentEntity entity : list){
            if(entity.getParentCode().equals(curVo.getCode())){
                DepartmentDictVo vo = new DepartmentDictVo();
                vo.setId(entity.getId());
                vo.setCode(entity.getCode());
                vo.setParentCode(entity.getParentCode());
                vo.setLabel(entity.getName());
                vo.setChildren(new ArrayList<>());
                curVo.getChildren().add(vo);
                process(vo,list);
            }
        }
    }

}